package com.cpi.cornpp.purchase.program.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.dto.PurchaseOrderDTO;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderAssignFutureView;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderView;
import com.cpi.cornpp.purchaseorder.dto.PurchaseOrderAssignFutureDTO;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;

public final class PurchaseOrderConverter {
	private static final String PADDING_PARAMETER = "PADDING_FOR_CODE";

	public static final PurchaseOrderView convert(PurchaseOrderDTO dto){
		PurchaseOrderView view = null;
		String postFix = null;
		SystemParameterBean param = (SystemParameterBean)SystemParameterCollection.getInstance().get(PADDING_PARAMETER);
		if (dto != null){
			view = new PurchaseOrderView();
			
			view.setPurchaseOrderId((dto.getPurchaseOrderId() != null) ? dto.getPurchaseOrderId().toString() : null);
			view.setPurchaseOrderWeekPeriodId((dto.getBiWeekPeriodId() != null) ? dto.getBiWeekPeriodId().toString() : null);
			view.setPurchaseOrderTransportId((dto.getTransportId() != null) ? dto.getTransportId().toString() : null);
			view.setPurchaseOrderPlantId((dto.getPlantId() != null) ? dto.getPlantId().toString() : null);
			view.setPurchaseOrderCode(dto.getCode()!=null?dto.getCode().trim():dto.getCode());
			postFix = String.valueOf(dto.getPostFix()).trim();			
			view.setPostFix(StringUtils.leftPad(postFix, param.getIntValue(),"0" ));
			view.setPurchaseOrderCapacity((dto.getCapacity() != null) ? dto.getCapacity().doubleValue() : 0.0D);
			view.setPurchaseOrderRealCapacity((dto.getRealCapacity() != null) ? dto.getRealCapacity().doubleValue() : 0.0D);
			view.setPurchaseOrderCapacityConverted(Math.round(dto.getCapacityConverted() + .4999));
			view.setBushelPrice(dto.getBushelPrice());
			view.setCornTypeId(String.valueOf(dto.getCornTypeId()));
		}
		
		return view;
	}
	
	public static final PurchaseOrderDTO convert(PurchaseOrderView view){
		PurchaseOrderDTO dto = null;
		
		if (view != null){
			dto = new PurchaseOrderDTO();
			
			dto.setPurchaseOrderId(StringUtils.isNotEmpty(view.getPurchaseOrderId()) ? Integer.valueOf(view.getPurchaseOrderId()) : null);
			dto.setBiWeekPeriodId(new Integer(view.getPurchaseOrderWeekPeriodId()));
			dto.setTransportId(StringUtils.isNotEmpty(view.getPurchaseOrderTransportId()) ? Integer.valueOf(view.getPurchaseOrderTransportId()) : null);
			dto.setPlantId(StringUtils.isNotEmpty(view.getPurchaseOrderPlantId()) ? Integer.valueOf(view.getPurchaseOrderPlantId()) : null);
			dto.setCode(view.getPurchaseOrderCode());
			dto.setPostFix(NumberUtil.parseInt(view.getPostFix()));		
			dto.setCapacity(new Double(view.getPurchaseOrderCapacity()));
			dto.setRealCapacity(new Double(view.getPurchaseOrderRealCapacity()));
			dto.setBushelPrice(view.getBushelPrice());
			dto.setCornTypeId(new Integer(NumberUtil.parseInt(view.getCornTypeId())));
		}
		
		return dto;
	}
	
	public static final List convertDTOList(List dtoList){
		PurchaseOrderDTO dto = null;
		List viewList = null;
		
		viewList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(dtoList)){
			
			for (Iterator itr = dtoList.iterator(); itr.hasNext(); ){
				dto = (PurchaseOrderDTO) itr.next();
				viewList.add(convert(dto));
			}
		}
		
		return viewList;
	}
	
	public static final List convertViewList(List viewList){
		PurchaseOrderView view = null;
		List dtoList = null;
		
		dtoList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(viewList)){
			
			for (Iterator itr = viewList.iterator(); itr.hasNext(); ){
				view = (PurchaseOrderView) itr.next();
				dtoList.add(convert(view));
			}
		}
		
		return dtoList;
	}
	
	public PurchaseOrderAssignFutureDTO converterAssignViewToAssignDTO(PurchaseOrderAssignFutureView purchaseOrderAssignFutureView, int purchaseOrderId) throws PurchaseProgramException{
		PurchaseOrderAssignFutureDTO purchaseOrderAssignFutureDTO = null;
		double quantity = -1;
		String quantityToConvert = null;
		try{
			purchaseOrderAssignFutureDTO = new PurchaseOrderAssignFutureDTO();
			purchaseOrderAssignFutureDTO.setPurchaseOrderId(purchaseOrderId);
			purchaseOrderAssignFutureDTO.setCornFutureId(purchaseOrderAssignFutureView.getCornFutureId());
			quantityToConvert = purchaseOrderAssignFutureView.getQuantity();
			if( quantityToConvert != null && !quantityToConvert.trim().equals(NumberUtil.EMPTY_STRING)){
				quantity = NumberUtil.parseDouble(quantityToConvert);
			}else{
				quantity = -1;
			}
			purchaseOrderAssignFutureDTO.setQuantity(quantity);
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_UPDATE);
			throw purchaseProgramException;
		}
		return purchaseOrderAssignFutureDTO;
		
		
	}
}